package com.systemframework.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.systemframework.properties.SaTokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * jwt配置
 * @author summer
 */
@Configuration
@EnableConfigurationProperties(SaTokenProperties.class)
@ConditionalOnClass({SaTokenProperties.class})
public class SaTokenConfigure implements WebMvcConfigurer {

    private SaTokenProperties saTokenProperties;

    @Autowired
    public void setSaTokenWrapper(SaTokenProperties saTokenProperties) {
        this.saTokenProperties = saTokenProperties;
    }

    @Bean
    public StpLogic getStpLogicJwt() {

        return new StpLogicJwtForSimple();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(saTokenProperties.getStaticPath()).addResourceLocations("file:"+ saTokenProperties.getFilePath());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns(saTokenProperties.getExcludePathLists());
    }
}
